// pages/department/index.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        departments: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDepartments()
    },
    getDepartments() {
        var _this = this
        //发送请求
        httpUtils.request({
            method: "GET",
            showLoading: true,
            url: `/api/department/list`,
            message: "正在获取...",
            // data:_this.data.card,
        }).then(res => {
            if (res.data.code != 0) {
                ui.showToast(res.data.mes)
            } else {
                _this.setData({
                    departments: res.data.data
                })
                for (var i = 0; i < _this.data.departments.length; i++) {
                    _this.data.departments[i].open = false
                }
            }
        }).catch(err => {
            console.log(err)
        });
    },
    kindToggle(e) {
        const { id } = e.currentTarget; const { departments } = this.data;
        for (let i = 0, len = departments.length; i < len; ++i) {
            if (departments[i].id == id) {
                departments[i].open = !departments[i].open;
            } else {
                departments[i].open = false;
            }
        }
        this.setData({
            departments,
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})